Micron Document




Java syntax
part 23/46 · 86.7 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
// Short syntax
int[] numbers2 = {20, 1, 42, 15, 34};

Multi-dimensional arrays

In Java, multi-dimensional arrays are represented as arrays of arrays. Technically, they are represented by arrays of references to other arrays.

int[][] numbers = new int[3][3];
numbers[1][2] = 2;
int[][] numbers2 = {{2, 3, 2}, {1, 2, 6}, {2, 4, 5}};

Due to the nature of the multi-dimensional arrays, sub-arrays can vary in length, so multi-dimensional arrays are not bound to be rectangular unlike C:

int[][] numbers = new int[2][]; //Initialization of the first dimension only
numbers[0] = new int[3];
numbers[1] = new int[2];

Classes

Classes are fundamentals of an object-oriented language such as Java. They contain members that store and manipulate data. Classes are divided into top-level and nested. Nested classes are classes placed inside another class that may access the private members of the enclosing class. Nested classes include member classes (which may be defined with the static modifier for simple nesting or without it for inner classes), local classes and anonymous classes.

Declaration

| Top-level class | class Foo { // Class members } |
|---|---|
| Inner class | class Foo { // Top-level class class Bar { // Inner class } } |
| Nested class | class Foo { // Top-level class static class Bar { // Nested class } } |
| Local class | class Foo { void bar () { class Foobar { // Local class within a method } } } |
| Anonymous class | class Foo { void bar () { new Object () { // Creation of a new anonymous class extending Object }; } } |

Instantiation

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────